﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.Base;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.ModelInterface;

namespace QLBH.FeatureModel
{
    public class NKMatHangModel : INKMatHangModel
    {
        public Guid PNKID { set; get; }
        public Guid MHID { set; get; }
        public String MHCODE { set; get; }
        public String TENMH { set; get; }        
        public String DVT { set; get; }
        private int soluong;
        public int SOLUONG 
        { 
            set
            {
                soluong = value;
                THANHTIEN = soluong * DONGIA - CHIETKHAU;
            }                   
            get
            {
                return soluong;
            }
        }
        public Decimal DONGIA { set; get; }
        private decimal chietkhau;
        public Decimal CHIETKHAU 
        { 
            set
            {
                chietkhau = value;
                THANHTIEN = soluong * DONGIA - CHIETKHAU;
            }
            get
            {
                return chietkhau;
            }
        }
        public Decimal THANHTIEN { set; get; }
        public String GHICHU { set; get; }
        public void InitFromEntity(PHIEUNHAPKHOCT entity)
        {
            PNKID = entity.PNKID;
            MHID = entity.MHID;

            MATHANG mh = MatHangController.GetInstance().FindBy(mhitem => mhitem.MHID == MHID).FirstOrDefault();

            if (mh!=null)
            {
                MHCODE = mh.MHCODE;
                TENMH = mh.MHTEN;
            }

            SOLUONG = (int)entity.SOLUONG;
            DONGIA = (Decimal)entity.DONGIA;
            CHIETKHAU = (Decimal)entity.CHIETKHAU;
            THANHTIEN = (Decimal)entity.THANHTIEN;
            GHICHU = entity.GHICHU;
        }

        public PHIEUNHAPKHOCT ToEntity()
        {
            PHIEUNHAPKHOCT entity = new PHIEUNHAPKHOCT();
            entity.CHIETKHAU = CHIETKHAU;
            entity.DONGIA = DONGIA;
            entity.GHICHU = GHICHU;
            entity.MHID = MHID;
            entity.PNKID = PNKID;
            entity.SOLUONG = SOLUONG;
            entity.THANHTIEN = THANHTIEN;


            return entity;
        }

        public void InitFromMatHangModel(MATHANG mh)
        {
            PNKID = Guid.NewGuid();
            MHID = mh.MHID;
            MHCODE = mh.MHCODE;
            TENMH = mh.MHTEN;
            DONGIA = (decimal)mh.GIANHAP;
            SOLUONG = 0;
            DONVITINH dvtobj = DVTController.GetInstance().FindBy(dvt => dvt.DVTID == mh.DVTID).FirstOrDefault();
            if (dvtobj != null)
            {
                DVT = dvtobj.TENDVT;                
            }
            CHIETKHAU = (decimal)NCC_MatHangController.GetInstance().FindBy(nccmh => nccmh.MHID == mh.MHID).FirstOrDefault().CHIETKHAU;
            GHICHU = "";
        }
    }
}
